package com.zd.kids.ui.activity;

import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.FileUtils;
import com.hyy.libray.presenter.IBasePresenter;
import com.hyy.libray.ui.activity.BaseActivity;
import com.zd.kids.DataBean.TestTrainDataBean;
import com.zd.kids.DataBean.TestWordDataBean;
import com.zd.kids.R;
import com.zd.kids.common.Constants;
import com.zd.kids.common.ToastUtils;
import com.zd.kids.net.CustomObserver;
import com.zd.kids.net.HttpMethod;
import com.zd.kids.ui.fragment.TestTrainFragment;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.FragmentTransaction;
import butterknife.BindView;
import butterknife.OnClick;

public class TestTrainActivity extends BaseActivity {
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.ll_start)
    LinearLayout llStart;
    @BindView(R.id.ll_start_bg)
    LinearLayout llStartBg;

    /**
     * 获取布局文件地址
     */
    @Override
    public int getLayoutResId() {
        return R.layout.activity_test_train;
    }


    public static final String AI_TEST_KEY = "AI_TEST_KEY";
    private String unitId;

    public static final String UNIT_ID_KEY = "UNIT_ID_KEY";

    private boolean isAiTest;

    private String mGroupid;

    private String mCategoryid;

    /**
     * 初始化布局
     */
    @Override
    public void initView() {
        initTitle("", R.color.cartoon_title_color);

        tvTips.setText(getString(R.string.are_you_ready));

        initPage2();

        unitId = getIntent().getStringExtra(UNIT_ID_KEY);

        isAiTest = getIntent().getBooleanExtra(AI_TEST_KEY, false);

        llStartBg.setBackgroundResource(!isAiTest ? R.drawable.shape_test_train_bg : R.drawable.shape_ai_test_train_bg);

        mGroupid = getIntent().getStringExtra(BookSeriesListActivity.GROUP_ID_KEY);

        mCategoryid = getIntent().getStringExtra(BookSeriesListActivity.CATEGORY_ID_KEY);

        if (isAiTest) {
            getAiTestTrainDatas(mGroupid, mCategoryid);
        } else {
            getTestWordsData(unitId);
        }

    }

    /**
     * 获取Presenter，不用MVP，可以为空
     *
     * @return
     */
    @Override
    protected IBasePresenter onBasePresenter() {
        return null;
    }


    @OnClick(R.id.ll_recorder)
    public void onViewClicked() {
        if (testTrainDataBeans.size() == 0) {
            ToastUtils.getIntence().makeTextShow(getString(R.string.data_error_tips));
        } else {
            imgBack.setVisibility(View.GONE);
            llStart.setVisibility(View.GONE);

            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.add(R.id.fl_contain, testTrainFragment)
                    .show(testTrainFragment)
                    .commit();
        }


    }


    private TestTrainFragment testTrainFragment;

    private void initPage2() {

        testTrainDataBeans = new ArrayList<>();

        testTrainFragment = new TestTrainFragment(testTrainDataBeans);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (imgBack.getVisibility() == View.GONE) {
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    List<TestTrainDataBean> testTrainDataBeans;


    private void getTestWordsData(String unitId) {

        showLoading();

        HttpMethod.getIntence().getTestWords(unitId, new CustomObserver<TestWordDataBean>(this) {
            @Override
            public void onSuccess(TestWordDataBean testWordDataBean) {
                disLoading();

                tvTitle.setText(testWordDataBean.getQuestion_name());

                testTrainFragment.setUnitId(unitId);

                testTrainFragment.setQuesName(testWordDataBean.getQuestion_name());

                testTrainDataBeans.addAll(testWordDataBean.getResult());

            }

            @Override
            public void onError(String errorCode) {
                disLoading();
            }
        });
    }


    @Override
    public void finish() {
        super.finish();

        FileUtils.deleteAllInDir(Constants.RECORD_ROOT_PATH);
    }


    private void getAiTestTrainDatas(String groupId, String categoryid) {
        showLoading();

        HttpMethod.getIntence().getAiTestTrainDatas(groupId, categoryid, new CustomObserver<List<TestTrainDataBean>>(this) {
            @Override
            public void onSuccess(List<TestTrainDataBean> trainDataBeans) {
                disLoading();


                testTrainFragment.setUnitId(unitId);

                testTrainFragment.setQuesName("");

                testTrainFragment.setAiRecord(true);

                testTrainFragment.setGroupid(mGroupid);

                testTrainFragment.setCategoryid(mCategoryid);


                if (testTrainDataBeans == null || trainDataBeans.size() <= 0) {
                    ToastUtils.getIntence().makeTextShow("數據有誤請聯系工作人員");
                }
                testTrainDataBeans.addAll(trainDataBeans);
            }

            @Override
            public void onError(String errorCode) {
                disLoading();


            }
        });


    }

}
